﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;



namespace NetDXY
{
    public partial class BaseFrm : Form
    {
        // Constant of
        const UInt32 OPEN_EXISTING = 3;
        const UInt32 GENERIC_READ = 0x80000000;
        const UInt32 GENERIC_WRITE = 0x40000000;

        const UInt32 OPEN_ALWAYS = 4;
        const UInt32 FILE_ATTRIBUTE_NORMAL = 0x00000080;
        const UInt32 FILE_FLAG_WRITE_THROUGH = 0x80000000;
        const Int32 INVALID_HANDLE_VALUE = -1;

        private UInt32 IOCTL_TSADC_CHANNEL(UInt32 i)
        {
            UInt32 METHOD_BUFFERED = 0;
            UInt32 FILE_ANY_ACCESS = 0;

            UInt32 FILE_DEVICE_UNKNOWN = 0x00000022;
            UInt32 BASENO = 800;

            //那个口就在800上增加
            BASENO = BASENO + i;


            UInt32 Value = 0;
            UInt32 DeviceType,Access, Function,Method;


            DeviceType = FILE_DEVICE_UNKNOWN;
            Access = FILE_ANY_ACCESS;

            Function = 0xA00 + BASENO;
            Method = METHOD_BUFFERED;

            Value = ((DeviceType) << 16) | ((Access) << 14) | ((Function) << 2) | (Method);


            return Value;
        }

        public IntPtr pPower;
        public BaseFrm()
        {            
            InitializeComponent(); 
        }

        private void SetTheDate(object sender, EventArgs e)
        {
            DateTime theDay = DateTime.Today;
            string xx;
            switch (theDay.DayOfWeek)
            {
               case DayOfWeek.Sunday: xx = "日";break;
               case DayOfWeek.Monday: xx = "一";break;
               case DayOfWeek.Tuesday: xx = "二";break;
               case DayOfWeek.Wednesday: xx = "三";break;
               case DayOfWeek.Thursday: xx = "四";break;
               case DayOfWeek.Friday: xx = "五";break;
               case DayOfWeek.Saturday: xx = "六";break;
               default: xx = "天"; break; 
            }





            lblDate.Text = theDay.ToString("yyyy年MM月dd日") + "  星期" + xx;
        }

        private void LoadFrm(object sender, EventArgs e)
        {
            Point pt = new Point(0, 0);
            pt.X = (Screen.PrimaryScreen.Bounds.Width - this.Width) / 2;
            pt.Y = (Screen.PrimaryScreen.Bounds.Height - this.Height) / 2;
            this.Location = pt;

            //pPower = DllInvoke.CreateFile("ADC1:", GENERIC_READ | GENERIC_WRITE, NULL, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, NULL);
            //pPower = DllInvoke.CreateFile("ADC1:", 0x80000000 | 0x40000000, null, null, 4, 0x00000080 | 0x80000000, null);
            //00000pPower = DllInvoke.CreateFile("ADC1:", GENERIC_READ | GENERIC_WRITE, 0, IntPtr.Zero, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_WRITE_THROUGH, IntPtr.Zero);

            tmDate.Enabled = true;
            //tmPower.Enabled = true;
        }

        private void GetPower(object sender, EventArgs e)
        {
            /*if (pPower != (IntPtr)INVALID_HANDLE_VALUE)
            { 
                byte[] sBuf = new byte[4];
                UInt32 ADCdata; //采样值

                DllInvoke.DeviceIoControlCE(pPower, IOCTL_TSADC_CHANNEL(0), null, 0, sBuf, 4, 0, IntPtr.Zero);
                ADCdata = BitConverter.ToUInt32(sBuf, 0);

                if (ADCdata >= 0xd50)
                {
                    pgPower.Value = 100;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xd00 && ADCdata < 0xd50)
                {
                    pgPower.Value = 90;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xcb0 && ADCdata < 0xd00)
                {
                    pgPower.Value = 80;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xc50 && ADCdata < 0xcb0)
                {
                    pgPower.Value = 70;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xc00 && ADCdata < 0xc50)
                {
                    pgPower.Value = 60;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xbb5 && ADCdata < 0xc00)
                {
                    pgPower.Value = 50;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xb60 && ADCdata < 0xbb5)
                {
                    pgPower.Value = 40;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xb10 && ADCdata < 0xb60)
                {
                    pgPower.Value = 30;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xac0 && ADCdata < 0xb10)
                {
                    pgPower.Value = 20;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xab8 && ADCdata < 0xac0)
                {
                    pgPower.Value = 10;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else if (ADCdata >= 0xa15 && ADCdata < 0xab8)
                {
                    pgPower.Value = 0;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
                else
                {
                    pgPower.Value = 0;
                    lblPowerVol.Text = pgPower.Value.ToString(); 
                }
            }*/

        }
    }
}